﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PauseMenu : MonoBehaviour
{

    public static int enemiesKilled = 0;
    public Rect windowLocation;
    public Rect labelPos = new Rect(GameGUI.Screen.width / 2, GameGUI.Screen.height / 2, 200f, 50f);
    public static bool pauseMenuEnabled = false;
    public OuterWindowButtonClass PauseButton;
    public LabelClass Fragments, WaveNo, TotalEnemies, EnemiesKilled;
    public InnerWindowButtonClass UpgradeStore, Resume, Main, Restart;
    public GUIStyle style;
    public GUIStyle ButtonLabel;
    private bool DoOnce = true;
    public Rect[] rects;
    public GUIStyle mystyle, soundStyle, vibrateStyle;
    private GameObject ToolTips;
    public Texture2D DownArrow, RightArrow;
    private bool isSound = false, isVibrate = false;
    public Rect soundLoc, vibrateLoc;
    public Texture2D soundTexture;
    public string soundText, vibrateText;
    private bool vibrateOnce = true;
    public static bool TutorialMode = false;
    // Use this for initialization

    [System.Serializable]
    public class InnerWindowButtonClass
    {
        public Rect ButtonLocation = new Rect(80f, 20f, 105f, 35f);
        public Texture2D Texture;
        public string Text = "If no texture use this";
    }

    [System.Serializable]
    public class OuterWindowButtonClass
    {
        public float x = 225f, y = 350f, width = 30f, height = 30f;
        public Texture2D Texture;
    }
    [System.Serializable]
    public class LabelClass
    {
        public Rect LabelLocation = new Rect(80f, 20f, 105f, 35f);
        public string text = "Enter text here";
        public float valueOffset = 100f;
        public GUIStyle labelStyle;
        //public Texture2D pauseTexture;
    }

    void Start()
    {
        isSound = Manager.Sound;
        if (Application.loadedLevel == 6)
        {
            TutorialMode = true;
            PlayerPrefs.SetInt("Currency", 800);
        }
        else
        {
            TutorialMode = false;
        }
        //rects = new System.Collections.Generic.List<Rect>();

        //rects = (new Rect(Screen.width / 2 - UpgradeStore.ButtonLocation.width / 2, Screen.height / 2 - UpgradeStore.ButtonLocation.height / 2, UpgradeStore.ButtonLocation.width, UpgradeStore.ButtonLocation.height));


    }

    void ToolTipMaint()
    {

        //if (moveGUItex.isToolTips)
        //{
        //    ToolTips = GameObject.FindGameObjectWithTag("ToolTipManager");
        //    if (ToolTips.transform.childCount > 0)
        //    {
        //        foreach (Transform child in ToolTips.transform)
        //        {
        //            if (child.gameObject.activeSelf)
        //            {
        //                ToolTips toolTip = child.gameObject.GetComponent<ToolTips>();
        //                if (toolTip.ButtonDestroy)
        //                {
        //                    Destroy(child.gameObject);
        //                }
        //            }
        //        }
        //    }
        //}
    }

    // Update is called once per frame
    void Update()
    {

    }
    void OnGUI()
    {
        GUI.depth = 2;
        if (DoOnce)
        {
            GUIStyle temp2 = ButtonLabel;
            ButtonLabel = new GUIStyle() ;
            ButtonLabel.font = temp2.font;
            ButtonLabel.normal.textColor = Color.white;
            ButtonLabel.fontSize = temp2.fontSize;
            ButtonLabel.alignment = temp2.alignment;
            GUIStyle temp = style;
            //			style = new GUIStyle(GUI.skin.GetStyle("Window")); //Or whatever
            //        	style.onNormal = temp.onNormal;
            //			style.normal.background = temp.normal.background;
            DoOnce = false;

        }
        style.onNormal = style.normal;
        Color tmpColor = GUI.color;
        //Use this to change alpha of pause button
        GUI.color = new Color(tmpColor.r, tmpColor.g, tmpColor.b, .9f);
        

        if (GUI.Button(new Rect(Screen.width /PauseButton.x, Screen.height /PauseButton.y, Screen.width/PauseButton.width, Screen.height/PauseButton.height), PauseButton.Texture, mystyle))
        {
          
            Manager.WavePause();
            pauseMenuEnabled = true;
            ToolTipMaint();
        }

        if (pauseMenuEnabled)
        {
            GUI.Window(0, new Rect(0, 0, Screen.width, Screen.height), DoWork, "", style);
        }
        GUI.color = tmpColor;


    }

    public void DoWork(int windowID)
    {/*
        //Labels
		Fragments.labelStyle.fontSize = 25;
        GUI.Label(Fragments.LabelLocation, Fragments.text, Fragments.labelStyle);
		GUI.Label(new Rect(Fragments.LabelLocation.x + Fragments.valueOffset, Fragments.LabelLocation.y, Fragments.LabelLocation.width, Fragments.LabelLocation.height), PlayerPrefs.GetInt("Currency").ToString(), Fragments.labelStyle);
		
		WaveNo.labelStyle.fontSize = 25;
        GUI.Label(WaveNo.LabelLocation, WaveNo.text, WaveNo.labelStyle);
        GUI.Label(new Rect(WaveNo.LabelLocation.x + WaveNo.valueOffset, WaveNo.LabelLocation.y, WaveNo.LabelLocation.width, WaveNo.LabelLocation.height), (Spawner.currWave + 1).ToString(), WaveNo.labelStyle);
		
		TotalEnemies.labelStyle.fontSize = 25;
        GUI.Label(TotalEnemies.LabelLocation, TotalEnemies.text, TotalEnemies.labelStyle);
        GUI.Label(new Rect(TotalEnemies.LabelLocation.x + TotalEnemies.valueOffset, TotalEnemies.LabelLocation.y, TotalEnemies.LabelLocation.width, TotalEnemies.LabelLocation.height), Spawner.waveEnemies.ToString(), TotalEnemies.labelStyle );
        
		EnemiesKilled.labelStyle.fontSize = 25;
        GUI.Label(EnemiesKilled.LabelLocation, EnemiesKilled.text, EnemiesKilled.labelStyle);
        GUI.Label(new Rect(EnemiesKilled.LabelLocation.x + EnemiesKilled.valueOffset, EnemiesKilled.LabelLocation.y, EnemiesKilled.LabelLocation.width, EnemiesKilled.LabelLocation.height), enemiesKilled.ToString(), EnemiesKilled.labelStyle);
		  */
        //Button

        for (int i = 0; i < 4; i++)
        {
            UpgradeStore.ButtonLocation.width = Screen.width / UpgradeStore.ButtonLocation.y;
            UpgradeStore.ButtonLocation.height = Screen.width / UpgradeStore.ButtonLocation.x;
            rects[i] = new Rect(Screen.width / 2 - UpgradeStore.ButtonLocation.width / 2, (Screen.height / 2 - UpgradeStore.ButtonLocation.height / 2 + 150f) - (10 + UpgradeStore.ButtonLocation.height) * i, UpgradeStore.ButtonLocation.width, UpgradeStore.ButtonLocation.height);
            GUI.DrawTexture(rects[i], Restart.Texture);
        }
        //GUI.DrawTexture(new Rect(Screen.width/2 - UpgradeStore.ButtonLocation.width/2,UpgradeStore.ButtonLocation.y, UpgradeStore.ButtonLocation.width, UpgradeStore.ButtonLocation.height),UpgradeStore.Texture);
        
        if (GUI.Button(rects[3], "UPGRADE", ButtonLabel))
        {
            pauseMenuEnabled = false;
            statMenu.statStart = true;
            UpgradeGUINew.upgradeEnabled = true;
            Manager.WavePause();
        }
        if (TutorialMode)
        {
            GUI.DrawTexture(new Rect(rects[3].x - rects[3].width/2, rects[3].y, rects[3].width / 2, rects[3].height), RightArrow);
        }
        //GUI.DrawTexture(Resume.ButtonLocation,Resume.Texture);

        if (Resume.Texture == null ? GUI.Button(Resume.ButtonLocation, Resume.Text) : GUI.Button(rects[2], "RESUME", ButtonLabel))
        {
            pauseMenuEnabled = false;
            Manager.WaveResume(true);


        }
            if (GUI.Button(rects[1], "RESTART", ButtonLabel))
            {
                //            pauseMenuEnabled = false;
                Manager.GameRestart();

            }
            //  GUI.DrawTexture(Main.ButtonLocation,Main.Texture);
            //wtf
            if (Main.Texture == null ? GUI.Button(Main.ButtonLocation, Main.Text) : GUI.Button(rects[0], "MAIN MENU", ButtonLabel))
            {
                Manager.MainMenu();
            }
            soundLoc.height = .1f * Screen.height;
            vibrateLoc.height = .1f * Screen.height;
            soundLoc.width = .1f * Screen.width;
            vibrateLoc.width = .1f * Screen.width;
            isSound = GUI.Toggle(new Rect(Screen.width*.88f, Screen.height*.05f, soundLoc.width, soundLoc.height), isSound, "", soundStyle);
            isVibrate = GUI.Toggle(new Rect(Screen.width*.02f, Screen.height*.05f, vibrateLoc.width, vibrateLoc.height), isVibrate, "", vibrateStyle);
            if (isVibrate && vibrateOnce)
            {
                Handheld.Vibrate();
                vibrateOnce = false;
            }
            else if (!isVibrate)
            {
                vibrateOnce = true;
            }
            Manager.Sound = isSound;
            Manager.Vibrate = isVibrate;
        }
    }
